floor12-notification.js ➔ processError   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
c 0
b 0
f 0
dl 0
loc 27
rs 8
1
f12notification = {
0 ignored issues
show
Bug introduced by
The variable f12notification seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.f12notification.
Loading history...
2
    infoListBlock: document.createElement('div'),
3
4
    notification: function (content, type) {
5
        f12notification.initNotificationBlock();
0 ignored issues
show
Bug introduced by
The variable f12notification seems to be never declared. If this is a global, consider adding a /** global: f12notification */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
6
7
        timeout = 3000
0 ignored issues
show
Bug introduced by
The variable timeout seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.timeout.
Loading history...
8
        svg = '<svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"/></svg>'
0 ignored issues
show
Bug introduced by
The variable svg seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.svg.
Loading history...
9
        info_class = 'info-object-info';
0 ignored issues
show
Bug introduced by
The variable info_class seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.info_class.
Loading history...
10
11
        if (type == 1) {
0 ignored issues
show
Best Practice introduced by
Comparing type to 1 using the == operator is not safe. Consider using === instead.
Loading history...
12
            info_class = 'info-object-success';
13
            timeout = 3000;
14
            svg = '<svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 512 512"><path fill="currentColor" d="M504 256c0 136.967-111.033 248-248 248S8 392.967 8 256 119.033 8 256 8s248 111.033 248 248zM227.314 387.314l184-184c6.248-6.248 6.248-16.379 0-22.627l-22.627-22.627c-6.248-6.249-16.379-6.249-22.628 0L216 308.118l-70.059-70.059c-6.248-6.248-16.379-6.248-22.628 0l-22.627 22.627c-6.248 6.248-6.248 16.379 0 22.627l104 104c6.249 6.249 16.379 6.249 22.628.001z"/></svg>'
15
16
        }
17
        if (type == 2) {
18
            info_class = 'info-object-error';
19
            timeout = 3000;
20
            svg = '<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="exclamation-circle" class="svg-inline--fa fa-exclamation-circle fa-w-16" role="img" viewBox="0 0 512 512"><path fill="currentColor" d="M504 256c0 136.997-111.043 248-248 248S8 392.997 8 256C8 119.083 119.043 8 256 8s248 111.083 248 248zm-248 50c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"/></svg>';
21
22
        }
23
24
        const info = document.createElement('div');
25
        info.classList.add('info-object');
26
        info.classList.add(info_class);
27
        info.innerHTML = svg + content;
28
        f12notification.infoListBlock.appendChild(info);
29
30
        setTimeout(function () {
31
            info.classList.add('info-hidden');
32
        }, timeout);
33
34
        setTimeout(function () {
35
            info.remove();
36
        }, timeout * 2);
37
    },
38
39
    success: function (content) {
40
        this.notification(content, 1)
41
    },
42
    error: function (content) {
43
        this.notification(content, 2)
44
    },
45
    info: function (content) {
46
        this.notification(content, 0)
47
    },
48
49
    initNotificationBlock: function () {
50
        if (document.getElementById('info-list') == null) {
0 ignored issues
show
Best Practice introduced by
Comparing document.getElementById("info-list") to null using the == operator is not safe. Consider using === instead.
Loading history...
51
            f12notification.infoListBlock.setAttribute('id', 'info-list')
0 ignored issues
show
Bug introduced by
The variable f12notification seems to be never declared. If this is a global, consider adding a /** global: f12notification */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
52
            document.body.appendChild(this.infoListBlock);
53
        }
54
    }
55
}
56
57
function info(content, type) {
58
    console.warn('Function info() is deprecated. Please, replace info() for f12notification.info(), f12notification.success() or f12notification.error().')
59
    return f12notification.notification(content, type);
60
}
61
62
63
// When something goes wrong, this function will try to parse error response body to recognize error text.
64
// After that it will show an error notification.
65
function processError(response) {
66
    if (typeof (response.responseJSON) === 'object') {
67
        f12notification.error(response.status + ': ' + response.responseJSON.message)
68
        return true;
69
    }
70
71
    if (response.responseText.length > 5) {
72
73
        if (response.responseText.length < 40) {
74
            f12notification.error(response.responseText);
75
            return true;
76
        }
77
78
        if (response.responseText.length > 40) {
79
            matches = response.responseText.match(/with message (.+)/);
0 ignored issues
show
Bug introduced by
The variable matches seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.matches.
Loading history...
80
81
            if (!matches)
82
                matches = response.responseText.match(/\): (.+)/);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
83
84
            if (matches) {
85
                f12notification.error(response.status + ': ' + matches[1].replace("&#039;", ""));
86
                return true;
87
            }
88
        }
89
    }
90
    f12notification.error(response.status + ': ' + response.statusText);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
91
}